//
//  DateFormatManger.m
//  AFNetworking
//
//  Created by abc on 2018/1/25.
//

#import "DateFormatManger.h"

@implementation DateFormatManger

#pragma mark - date format translations
+ (NSDateFormatter *)dateFormatterForStyle:(NSDateFormatterStyle)style {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = style;
    
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [dateFormatter setLocale:locale];
    
    return dateFormatter;
}

+ (NSDateFormatter *)dateFormatterForFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [dateFormatter setLocale:locale];
    
    return dateFormatter;
}

+ (NSDateFormatter *)dateFormatterForMode:(UIDatePickerMode)dateMode {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    switch (dateMode) {
        case UIDatePickerModeTime:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        case UIDatePickerModeDate:
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            break;
        case UIDatePickerModeDateAndTime:
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            break;
        case UIDatePickerModeCountDownTimer:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        default:
            break;
    }
    
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [dateFormatter setLocale:locale];
    
    return dateFormatter;
}

+ (NSString *)stringOfDate:(NSDate *)date forStyle:(NSDateFormatterStyle)style {
    return date ? [[DateFormatManger dateFormatterForStyle:style] stringFromDate:date] : nil;
}

+ (NSDate *)dateOfString:(NSString *)string forStyle:(NSDateFormatterStyle)style {
    return string ? [[DateFormatManger dateFormatterForStyle:style] dateFromString:string] : nil;
}

+ (NSString *)stringOfDate:(NSDate *)date forFormat:(NSString *)format {
    return date ? [[DateFormatManger dateFormatterForFormat:format] stringFromDate:date] : nil;
}

+ (NSDate *)dateOfString:(NSString *)string forFormat:(NSString *)format {
    return string ? [[DateFormatManger dateFormatterForFormat:format] dateFromString:string] : nil;
}

+ (NSString *)stringOfDate:(NSDate *)date forMode:(UIDatePickerMode)mode {
    return date ? [[DateFormatManger dateFormatterForMode:mode] stringFromDate:date] : nil;
}

+ (NSDate *)dateOfString:(NSString *)string forMode:(UIDatePickerMode)mode {
    return string ? [[DateFormatManger dateFormatterForMode:mode] dateFromString:string] : nil;
}

#pragma mark - date compare functions
+ (BOOL)isCurrentBehindDate:(NSString *)end_time {
    NSDate *end = [self dateOfString:end_time forFormat:@"yyyy-MM-dd HH:mm:ss"];
    if ([NSDate.date compare:end] == NSOrderedDescending || [NSDate.date compare:end] == NSOrderedSame) {
        return YES;
    }
    return NO;
}

+ (BOOL)isCurrentBeforeDate:(NSString *)start_time {
    NSDate *start = [self dateOfString:start_time forFormat:@"yyyy-MM-dd HH:mm:ss"];
    if ([NSDate.date compare:start] == NSOrderedAscending) {
        return YES;
    }
    return NO;
}

+ (BOOL)isCurrentBetweenDate:(NSString *)start_time andDate:(NSString *)end_time {
    NSDate *start = [self dateOfString:start_time forFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *end = [self dateOfString:end_time forFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    if ( ([NSDate.date compare:start] == NSOrderedDescending || [NSDate.date compare:start] == NSOrderedSame) && ([NSDate.date compare:end] == NSOrderedAscending) ) {
        return YES;
    }
    if ( ([NSDate.date compare:start] == NSOrderedAscending) && ([NSDate.date compare:end] == NSOrderedDescending) ) {
        return YES;
    }
    return NO;
}

+ (NSString *)timeSapcingStringForDateString:(NSString *)serverTime {
    
    NSDate *serverDate = [DateFormatManger dateOfString:serverTime forFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSInteger timeSpacing = [NSDate.date timeIntervalSinceDate:serverDate];
    NSInteger timeResidue = 0;
    
    if (timeSpacing < 60) {
        return [NSString stringWithFormat:@"%ld %@", (long)timeSpacing, @"sec. ago"];
    }
    else if( (timeResidue = timeSpacing / 60) < 60 ) {
        return [NSString stringWithFormat:@"%ld %@", (long)timeResidue, @"min. ago"];
    }
    else if( (timeResidue = timeResidue / 60) < 24 ) {
        return [NSString stringWithFormat:@"%ld %@", (long)timeResidue, timeResidue == 1 ? @"hour ago" : @"hours ago"];
    }
    else if( (timeResidue = timeResidue / 24) < 30 ) {
        return [NSString stringWithFormat:@"%ld %@", (long)timeResidue, timeResidue == 1 ? @"day ago" : @"days ago"];
    }
    else if( (timeResidue = timeResidue / 30) < 12 ) {
        return [NSString stringWithFormat:@"%ld %@", (long)timeResidue, timeResidue == 1 ? @"month ago" : @"months ago"];
    }
    else{
        return [NSString stringWithFormat:@"%ld %@", (long)timeResidue/12, timeResidue/12 == 1 ? @"year ago" : @"years ago"];
    }
    
    BOOL isToday = [[NSCalendar currentCalendar] isDateInToday:NSDate.date];
}

@end
